﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Text;
using System.Windows.Input;

namespace SteveDower.SimpleBacklog
{
    public enum Priority
    {
        Critical,
        Important,
        Wishlist
    }

    public enum Status : int
    {
        Testing = 0,
        Working = 1,
        Deferred = 2,
        Unstarted = 3,
        Completed = 4
    }
    
    public class BacklogItem : DependencyObject, IComparable, IComparable<BacklogItem>
    {
        public BacklogItem(Priority priority)
        {
            Index = ++NextIndex;
            Status = Status.Unstarted;
            Priority = priority;
            Text = "";
            Category = "";
            Notes = "";
        }


        #region Properties

        private static int NextIndex = 0;
        public int Index { get; set; }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(BacklogItem), new UIPropertyMetadata(""));



        public string Category
        {
            get { return (string)GetValue(CategoryProperty); }
            set { SetValue(CategoryProperty, value); }
        }

        public static readonly DependencyProperty CategoryProperty =
            DependencyProperty.Register("Category", typeof(string), typeof(BacklogItem), new UIPropertyMetadata(""));



        public string Notes
        {
            get { return (string)GetValue(NotesProperty); }
            set { SetValue(NotesProperty, value); }
        }

        public static readonly DependencyProperty NotesProperty =
            DependencyProperty.Register("Notes", typeof(string), typeof(BacklogItem), new UIPropertyMetadata(""));



        public Priority Priority
        {
            get { return (Priority)GetValue(PriorityProperty); }
            set { SetValue(PriorityProperty, value); }
        }

        public static readonly DependencyProperty PriorityProperty =
            DependencyProperty.Register("Priority", typeof(Priority), typeof(BacklogItem), new UIPropertyMetadata(Priority.Wishlist));



        public Status Status
        {
            get { return (Status)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(Status), typeof(BacklogItem), new UIPropertyMetadata(Status.Unstarted));

        #endregion


        #region IComparable<BacklogItem> Members

        public int CompareTo(object obj)
        {
            return CompareTo(obj as BacklogItem);
        }

        public int CompareTo(BacklogItem other)
        {
            if (other == null) return -1;
            int cmp = Status.CompareTo(other.Status);
            if (cmp != 0) return cmp;
            cmp = Priority.CompareTo(other.Priority);
            if (cmp != 0) return cmp;
            cmp = Category.CompareTo(other.Category);
            if (cmp != 0) return cmp;
            return Text.CompareTo(other.Text);
        }

        #endregion

        #region Parse/Write Members

        public static BacklogItem Parse(string source)
        {
            var bi = new BacklogItem(Priority.Wishlist);
            var parts = new List<string>();
            var current = "";
            bool escaping = false;
            foreach (var c in source)
            {
                if (escaping)
                {
                    current += c;
                    escaping = false;
                }
                else if (c == ',')
                {
                    parts.Add(current);
                    current = "";
                }
                else if (c == '\\')
                {
                    escaping = true;
                }
                else
                {
                    current += c;
                }
            }
            parts.Add(current);

            if(parts.Count != 6) throw new FormatException(source + " is not a valid line.");

            bi.Index = int.Parse(parts[0]);
            if (bi.Index > NextIndex) NextIndex = bi.Index;
            bi.Priority = (Priority)Enum.Parse(typeof(Priority), parts[1]);
            bi.Status = (Status)Enum.Parse(typeof(Status), parts[2]);
            bi.Text = parts[3];
            bi.Category = parts[4];
            bi.Notes = parts[5];

            return bi;
        }

        private static string Escape(string source)
        {
            return source.Replace("\\", "\\\\").Replace(",", "\\,");
        }

        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3},{4},{5}",
                Index, Priority, Status,
                Escape(Text), Escape(Category), Escape(Notes));
        }

        #endregion
    }

}
